// AJAX INIT
function $$$(id) {
	return document.getElementById(id);
}
function khoitao_ajax()
{
	var x;
	try 
	{
		x	=	new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
    	try 
		{
			x	=	new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(f) { x	=	null; }
  	}
	if	((!x)&&(typeof XMLHttpRequest!="undefined"))
	{
		x=new XMLHttpRequest();
  	}
	return  x;
}
$.extend({
  ajaxIt : function(query, lnk, callbackFnk){    
	var http 	=	khoitao_ajax();
	try
	{
	  http.open("POST", lnk);
	  http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	  http.setRequestHeader("Cache-control", "no-cache");		
	  http.onreadystatechange = function()
	  {
		  if (http.readyState == 4)
		  {
			  if (http.status == 200)
			  {
				  $(".jGrowl-notification .jGrowl-message").each(function(i,e){
						if ($(this).text() == "Processing...")
						{
							$(this).parent().hide();
						}
					});
				  // Callback
				  if(typeof callbackFnk == 'function'){
					  x = http.responseText;
					  callbackFnk.call(this, x);
				  }
				  //---------
			   }
				else
				{
					alert('Có lỗi hệ thống. Xin vui lòng thử lại sau');
				}
			}
		}
		http.send(query);
	}
	catch (e)
	{
	}
	return false;
  }
});

function $query(obj)
{
	var query = "";
	$(obj).find("input").each(function(i){
		if (($(obj).find("input").eq(i).attr("type") != "checkbox") && ($(obj).find("input").eq(i).attr("type") != "button") && ($(obj).find("input").eq(i).attr("type") != "submit") && ($(obj).find("input").eq(i).attr("type") != "radio") )
		{
			var t = $(obj).find("input").eq(i);
			query += "&"+t.attr("name")+"="+encodeURIComponent(t.attr("value"));
		}
		else
		{
			if ($(obj).find("input").eq(i).attr("type") == "checkbox")
			{
				var t = $(obj).find("input").eq(i);
				query += "&"+t.attr("name")+"="+t.attr("checked");
			}
			else if ($(obj).find("input").eq(i).attr("type") == "radio")
			{
				var t = $(obj).find("input").eq(i);
				if (t.is(":checked"))
					query += "&"+t.attr("name")+"="+t.attr("value");
			}
		}
	});
	
	$(obj).find("textarea").each(function(i){
		var t = $(obj).find("textarea").eq(i);
		query += "&"+t.attr("id")+"="+encodeURIComponent(t.attr("value"));
	});
	
	$(obj).find("select").each(function(i){
		var t = $(obj).find("select").eq(i);
		query += "&"+t.attr("id")+"="+encodeURIComponent(t.attr("value"));
	});
	
	return query.substring(1);
}

function	Forward(url)
{
	window.location.href = url;
}
function	_postback()
{
	return void(1);
}

jQuery(window).load(function(){ 
	Cufon.replace('.cufon', {fontFamily:'utm', hover:true});
});

function send_contact(contactfrm)
{
	$$$("notify").innerHTML = '<img src="/images/ajax-loader.gif" align="absmiddle" border="0" /> Thông tin của bạn đang được gởi đi.';
	
	var ten = contactfrm.txt_ten.value;
	
	var email = contactfrm.txt_email.value;
	
	var tieu_de = contactfrm.txt_tieu_de.value;
	
	var dia_chi = contactfrm.txtAddress.value;
	
	var dien_thoai = contactfrm.txtTel.value;
	
	var noi_dung = contactfrm.txt_noi_dung.value;
	
	if (!ten || ten.length < 8)
	{
		document.contactfrm.txt_ten.focus();
		$$$("notify").innerHTML = '<span style="color:#C00">Tên từ 8 ký tự trở lên</span> !';
		return false;
	}
	
	else if (!email.match(/^([-\d\w][-.\d\w]*)?[-\d\w]@([-\w\d]+\.)+[a-zA-Z]{2,6}$/))
	{
		document.contactfrm.txt_email.focus();
		$$$("notify").innerHTML = '<span style="color:#C00">Email không đúng</span> !';
		return false;
	}	
	else if (!tieu_de || tieu_de.length < 8)
	{
		document.contactfrm.txt_tieu_de.focus();
		$$$("notify").innerHTML = '<span style="color:#C00">Tiêu đề quá ngắn</span> !';
		return false;
	}	
	else if (!noi_dung || noi_dung.length < 16)
	{
		document.contactfrm.txt_noi_dung.focus();
		$$$("notify").innerHTML = '<span style="color:#C00">Nội dung quá ngắn</span> !';
		return false;
	}		
	else
	{
	}

	var	query	=	"act=contact&txt_ten="+ten+"&txt_tieu_de="+tieu_de+"&txt_noi_dung="+noi_dung+"&txt_email="+email+"&txtAddress="+dia_chi+'&txtTel='+dien_thoai;
	var http 	=	khoitao_ajax();
	try
	{
		http.open("POST", "/action.php");
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Cache-control", "no-cache");		
    	http.onreadystatechange = function()
		{
			if (http.readyState == 4)
			{
				if (http.status == 200)
				{
					x = http.responseText;
					if (x != "OK")
					{
						$$$("notify").innerHTML = '<span style="color:#C00">'+x+'</span> !';
					}
					else
					{
						$$$("notify").innerHTML = '<span style="color:#C00">Thông tin của bạn đã được gửi đi</span> !';
					}
				}
				else
				{
					$$$("notify").innerHTML = '<span style="color:#C00">Có lỗi hệ thống. Xin vui lòng thử lại sau</span> !';
				}
			}
		}
		http.send(query);
	}
	catch (e)
	{
		$$$("notify").innerHTML = '<span style="color:#C00">Đang có lỗi hệ thống!</span><br>Vui lòng thử lại sau !';
	}
	return false;
	
}

function show_notify(msg)
{
	$.ajaxIt("act=show_thong_bao&msg="+encodeURIComponent(msg),'/action.php',function(x)
	{
		$("#luu_result").html(x);
	});
}

$(document).ready(function()
{
	$("a[rel=example_group]").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	true
	});
	
	$(".tbl_transform").jqTransform();
	
	$(".date_picker").datepicker({changeMonth:true, changeYear:true, dateFormat: 'dd-mm-yy', minDate: +0 });
	$(".time_picker").datetimepicker({showButtonPanel:false,stepMinute: 10, dateFormat: 'dd-mm-yy', minDate: +0});;
	
	$(".tour_ideas_menu_item").hover(function()
	{
		if (!$(this).hasClass('active'))
		{
			var rel = $(this).attr('rel');
			$(".tour_ideas_menu_item").removeClass('active');
			$(".tour_ideas_bg").removeClass('bg_active');
			$(this).addClass('active');
			$(".tour_ideas_bg[rel="+rel+"]").addClass('bg_active');
		}
	});
	
	
});

var $container;
		
$(document).ready(function() 
{
	$container = $("#container");
	$container.wtRotator({
		width				: 450,
		height				: 180,
		border				: "none",
		button_width		: 14,
		button_height		: 14,
		button_margin		: 8,
		auto_start			: true,
		delay				: 5000,
		transition			: "random",
		transition_speed	: 800,
		block_size			: 100,
		vert_size			: 50,
		horz_size			: 50,
		display_thumbs		: true,
		display_dbuttons	: false,
		display_playbutton	: false,
		display_tooltip		: false,
		display_numbers		: false,
		cpanel_mouseover	: true,
		text_mouseover		: false
	});
});

function reset_index()
{
	var zIndex = 100;
	$(".jqTransformSelectWrapper").each(function()
	{
		$(this).css('z-index',zIndex);
		zIndex --;
	});
}

function reload_location(country_id,sbo_id,name)
{
	var query = 'act=reload_location&country_id='+country_id+"&name="+name;
	$.ajaxIt(query,'/action.php',function(x)
	{
		x = $.trim(x);
		$("#"+sbo_id+"").html(x);
		$("#"+sbo_id+"").removeClass('jqtransformdone');
		$("#"+sbo_id+"").jqTransform();
		reset_index();
	});
}

$(document).ready(function()
{
	$(".search_menu_item").click(function()
	{
		if (!$(this).hasClass('active'))
		{
			this_item = $(this);
			$(".search_content").css('opacity','0.5');
			mod = $(this).attr('rel');
			var query = 'act=search_load_content&mod='+mod;
			$.ajaxIt(query,'/action.php',function(x)
			{
				x = $.trim(x);
				$(".search_content").html(x);
				$(".search_content").css('opacity','1');
				$(".search_menu_item").removeClass('active');
				$(this_item).addClass('active');
				$(".search_content").removeClass('jqtransformdone');
				$(".search_content").jqTransform();
			});
		}
	});
});